<template>
    <div class="common-layout">
        <el-container>
            <el-header class="homeHeader">
                <div class="title">Cloud E Project</div>
                <el-dropdown class="userInfo" @command="selectedHandler">
                    <span class="el-dropdown-link">
                      {{user.name}} <i><img :src="user.avatar"/></i>
                    </span>
                    <template #dropdown>
                        <el-dropdown-menu>
                            <el-dropdown-item command="userInfo">个人中心</el-dropdown-item>
                            <el-dropdown-item command="setting">设置</el-dropdown-item>
                            <el-dropdown-item command="logout">注销登录</el-dropdown-item>
                        </el-dropdown-menu>
                    </template>
                </el-dropdown>
            </el-header>
            <el-container>
                <el-aside width="200px">
                    <el-menu router unique-opened><!--@select="menuClick"-->
                        <el-sub-menu :index="index+''" v-for="(item,index) in routes" :key="index">
                            <template #title>
                                <i :class="item.iconCls" style="margin-right: 8px;color:#abcdef;"></i>
                                <span>{{item.name}}</span>
                            </template>
                            <el-menu-item :index="children.path" v-for="(children, cIndex) in item.children"
                                          :key="cIndex">{{children.name}}
                            </el-menu-item>
                        </el-sub-menu>
                    </el-menu>
                </el-aside>
                <el-main>
                    <el-breadcrumb separator="/" v-if="getRouteName!='首页'">
                        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
                        <el-breadcrumb-item><a href="/">{{this.$route.name}}</a></el-breadcrumb-item>
                    </el-breadcrumb>
                    <div v-if="getRouteName=='首页'" class="homeWelcome">
                        欢迎来到 Cloud E Project
                    </div>
                    <router-view class="homeRouterView"/>
                </el-main>
            </el-container>
        </el-container>
    </div>
</template>

<script>
    import {store} from "../store";
    import { ElMessageBox } from 'element-plus'
    import router from '../router'
    import {postRequest} from "../http";

    export default {
        name: "Home",
        data(){
            return {
                user: {
                    name:'admin',
                    avatar:'https://tse4-mm.cn.bing.net/th/id/OIP-C.oAofaiHIlMiepMHxvXxwyAHaHa?pid=ImgDet&rs=1'
                },
                routeName: '',
            }
        },
        computed:{
            routes(){
                let routes = store.state.routes;
                return routes;
            }
        },
        methods:{
            selectedHandler(cmd){
                if(cmd == 'logout'){
                    ElMessageBox.confirm(
                        '你确定要退出吗?',
                        '系统提示',
                        {
                            confirmButtonText:"确认",
                            cancelButtonText:"取消",
                            type:'warning'
                        }
                    ).then(()=>{
                        postRequest('/api/AuthManagement/logout');
                        window.sessionStorage.removeItem('user');
                        window.sessionStorage.removeItem('tokenStr');
                        // 清除菜单
                        store.commit('initialRoutes', []);
                        router.replace('/');
                    }).catch(()=>{

                    });
                }
            }
        }
    }
</script>

<style scoped>
    .homeHeader {
        background: #409eff;
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 0 15px;
        box-sizing: border-box;
    }

    .homeHeader .title {
        font-size: 30px;
        font-family: Consolas;
        color: white;
    }
    .homeHeader .userInfo{
        cursor: pointer;
    }
    .el-dropdown-link img{
        width: 48px;
        height: 48px;
        border-radius: 50%;
        margin-left: 8px;
    }
    .homeWelcome{
        text-align: center;
        font-size: 30px;
        font-family: 华文楷体;
        color: #409eff;
        margin-top: 60px;
    }
    .homeRouterView{
        margin-top: 10px;
    }
</style>
